﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using APPLICATION.Departments;

namespace APPLICATION.Dict.EBK
{
    public partial class frmEBK : Form
    {
        bool isDefaultText;
        bool isKeyPressed;
        public frmEBK()
        {
            InitializeComponent();
        }

        private void btnRefreshDep_Click(object sender, EventArgs e)
        {
            if (ckbEBK.Checked)
            {
                Fill_EBK();
            }
            else
            {
                Fill_FBK();
            }
            //System.Data.SqlClient.SqlConnection cnSQL=new SqlConnection(globals.APPCONNSTRING);
            //System.Data.SqlClient.SqlDataAdapter daSQL=new SqlDataAdapter();
            //System.Data.SqlClient.SqlCommand cmdSQL=new SqlCommand();

            //cmdSQL.CommandText = "tsp_dtEBK";
            //cmdSQL.CommandType = CommandType.StoredProcedure;
            //cmdSQL.Connection = cnSQL;

            //daSQL.SelectCommand = cmdSQL;

            //try
            //{
            //    this.timsDS.tsp_dtEBK.Clear();
            //    cnSQL.Open();
            //    daSQL.Fill(this.timsDS.tsp_dtEBK);
            //    this.treeList1.ExpandAll();
            //    this.treeList1.Focus();
            //    cnSQL.Close();
            //}

            //catch(Exception ex)
            //{
            //    MessageBox.Show("Ошибка при загрузке данных! \n" + ex.Message, "TIMS");
            //}
        }

        private void frmEBK_Shown(object sender, EventArgs e)
        {
            btnRefreshDep_Click(sender,e);
        }

        private void btnDeleteEBK_Click(object sender, EventArgs e)
        {
            if (ckbFBK.Checked)
            {
                int intRecordCount = tspdtFBKBindingSource.Count;

                if (intRecordCount > 0)
                {
                    string hName = this.treeList1.Selection[0].GetValue("hName").ToString();
                    DialogResult drs;
                    APPLICATION.Departments.frmDelete frmDel = new frmDelete(hName);
                    drs = frmDel.ShowDialog();

                    //if (MessageBox.Show("Вы действительно хотите удалить "+hName, "TIMS", MessageBoxButtons.YesNo, MessageBoxIcon.Question)==System.Windows.Forms.DialogResult.Yes)
                    if (drs == System.Windows.Forms.DialogResult.OK)
                    {
                        int intID = Convert.ToInt32((object)this.treeList1.Selection[0].GetValue("CodeID"));
                        System.Data.SqlClient.SqlCommand cmdSQL = new SqlCommand();
                        System.Data.SqlClient.SqlConnection cnSQL = new SqlConnection();

                        cnSQL.ConnectionString = globals.APPCONNSTRING;
                        cmdSQL.CommandText = "tsp_dtFBK";
                        cmdSQL.CommandType = CommandType.StoredProcedure;
                        cmdSQL.Parameters.Add("@Action", SqlDbType.TinyInt).Value = 2;
                        cmdSQL.Parameters.Add("@CodeID", SqlDbType.Int).Value = intID;
                        cmdSQL.Connection = cnSQL;

                        try
                        {
                            cnSQL.Open();
                            cmdSQL.ExecuteNonQuery();
                            cnSQL.Close();
                            btnRefreshDep_Click(sender, e);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Ошибка при удалении записи! \n" + ex.Message, "TIMS");
                        }
                    }
                }
            }
            else
            {
                int intRecordCount = tspdtEBKBindingSource.Count;

                if (intRecordCount > 0)
                {
                    string hName = this.treeList1.Selection[0].GetValue("hName").ToString();
                    DialogResult drs;
                    APPLICATION.Departments.frmDelete frmDel = new frmDelete(hName);
                    drs = frmDel.ShowDialog();

                    //if (MessageBox.Show("Вы действительно хотите удалить "+hName, "TIMS", MessageBoxButtons.YesNo, MessageBoxIcon.Question)==System.Windows.Forms.DialogResult.Yes)
                    if (drs == System.Windows.Forms.DialogResult.OK)
                    {
                        int intID = Convert.ToInt32((object)this.treeList1.Selection[0].GetValue("CodeID"));
                        System.Data.SqlClient.SqlCommand cmdSQL = new SqlCommand();
                        System.Data.SqlClient.SqlConnection cnSQL = new SqlConnection();

                        cnSQL.ConnectionString = globals.APPCONNSTRING;
                        cmdSQL.CommandText = "tsp_dtEBK";
                        cmdSQL.CommandType = CommandType.StoredProcedure;
                        cmdSQL.Parameters.Add("@Action", SqlDbType.TinyInt).Value = 2;
                        cmdSQL.Parameters.Add("@CodeID", SqlDbType.Int).Value = intID;
                        cmdSQL.Connection = cnSQL;

                        try
                        {
                            cnSQL.Open();
                            cmdSQL.ExecuteNonQuery();
                            cnSQL.Close();
                            btnRefreshDep_Click(sender, e);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Ошибка при удалении записи! \n" + ex.Message, "TIMS");
                        }
                    }
                }
            }
        }

        private void textBoxSearch_TextChanged(object sender, EventArgs e)
        {
            if (isKeyPressed)
            {
                tspdtEBKBindingSource.Filter = "hName like '%" + textBoxSearch.Text + "%' OR hccrt like '%" + textBoxSearch.Text + "%'";
                tspdtFBKBindingSource.Filter = "hName like '%" + textBoxSearch.Text + "%' OR hccrt like '%" + textBoxSearch.Text + "%'";

                //if (ckbEBK.Checked)
                //{
                //    tspdtEBKBindingSource.Filter = "hName like '%" + textBoxSearch.Text + "%' OR hccrt like '%" + textBoxSearch.Text + "%'";
                //}
                //else
                //{
                //    tspdtFBKBindingSource.Filter = "hName like '%" + textBoxSearch.Text + "%' OR hccrt like '%" + textBoxSearch.Text + "%'";
                //}
            }
            if (isKeyPressed && string.IsNullOrEmpty(textBoxSearch.Text))
            {
                tspdtEBKBindingSource.Filter = "";
                tspdtFBKBindingSource.Filter = "";
            }
            isKeyPressed = false;
        }

        private void textBoxSearch_KeyDown(object sender, KeyEventArgs e)
        {
            isKeyPressed = true;

            if (e.KeyValue == 40 || e.KeyValue==38 || e.KeyValue==13) 
            {
                this.treeList1.Focus(); 
            }
            //MessageBox.Show(e.KeyValue.ToString());
        }

        private void textBoxSearch_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxSearch.Text))
            {
                textBoxSearch.Text = "Быстрый поиск по имени";
                textBoxSearch.ForeColor = Color.Gray;
                isDefaultText = true;
            }
            else
            {
                isDefaultText = false;

            }
        }

        private void textBoxSearch_Enter(object sender, EventArgs e)
        {

            if (isDefaultText || textBoxSearch.Text == "Быстрый поиск по имени")
            {
                textBoxSearch.Text = "";
                textBoxSearch.ForeColor = Color.Black;
            }
        }

        private void ckbEBK_CheckedChanged(object sender, EventArgs e)
        {
            //textBoxSearch.Text = "";

            if (ckbEBK.Checked == true)
            {
                ckbFBK.Checked = false;
                Fill_EBK();
            }
            else
            {
                ckbFBK.Checked = true;
            }
        }

        private void ckbFBK_CheckedChanged(object sender, EventArgs e)
        {
            //textBoxSearch.Text = "";

            if (ckbFBK.Checked == true)
            {
                ckbEBK.Checked = false;
                Fill_FBK();
            }
            else
            {
                ckbEBK.Checked = true;
            }
        }

        private void Fill_FBK()
        {
            System.Data.SqlClient.SqlConnection cnSQL = new SqlConnection(globals.APPCONNSTRING);
            System.Data.SqlClient.SqlDataAdapter daSQL = new SqlDataAdapter();
            System.Data.SqlClient.SqlCommand cmdSQL = new SqlCommand();

            cmdSQL.CommandText = "tsp_dtFBK";
            cmdSQL.CommandType = CommandType.StoredProcedure;
            cmdSQL.Connection = cnSQL;

            daSQL.SelectCommand = cmdSQL;

            try
            {
                this.timsDS.tsp_dtFBK.Clear();
                cnSQL.Open();
                daSQL.Fill(this.timsDS.tsp_dtFBK);
                //this.treeList1.DataSource = this.timsDS.tsp_dtFBK;

                this.treeList1.DataSource = this.tspdtFBKBindingSource;

                this.treeList1.ExpandAll();
                this.treeList1.Focus();
                cnSQL.Close();
            }

            catch (Exception ex)
            {
                MessageBox.Show("Ошибка при загрузке данных! \n" + ex.Message, "TIMS");
            }
        }

        private void Fill_EBK()
        {
            System.Data.SqlClient.SqlConnection cnSQL = new SqlConnection(globals.APPCONNSTRING);
            System.Data.SqlClient.SqlDataAdapter daSQL = new SqlDataAdapter();
            System.Data.SqlClient.SqlCommand cmdSQL = new SqlCommand();

            cmdSQL.CommandText = "tsp_dtEBK";
            cmdSQL.CommandType = CommandType.StoredProcedure;
            cmdSQL.Connection = cnSQL;

            daSQL.SelectCommand = cmdSQL;

            try
            {
                this.timsDS.tsp_dtEBK.Clear();
                cnSQL.Open();
                daSQL.Fill(this.timsDS.tsp_dtEBK);

                //this.treeList1.DataSource = this.timsDS.tsp_dtEBK;
                this.treeList1.DataSource = this.tspdtEBKBindingSource;

                this.treeList1.ExpandAll();
                this.treeList1.Focus();
                cnSQL.Close();
            }

            catch (Exception ex)
            {
                MessageBox.Show("Ошибка при загрузке данных! \n" + ex.Message, "TIMS");
            }
        }

        private void treeList1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control == true)
            {
                if (e.KeyValue==70)
                {
                    textBoxSearch.Focus();
                }
                 //MessageBox.Show("Control pressed: " + e.KeyValue.ToString());
            }
            //else { MessageBox.Show(e.KeyValue.ToString()); }
        }

        private void btnEditEBK_Click(object sender, EventArgs e)
        {
            frmEdit_EBKFBK frmed=new frmEdit_EBKFBK();
            frmed.Text = "Редактирование";

            if (ckbFBK.Checked)
            {
                frmed.strSPName = "tsp_dtFBK";
                frmed.bs = tspdtFBKBindingSource;
                frmed.dt = this.timsDS.tsp_dtFBK;
            }
            else
            {
                frmed.strSPName = "tsp_dtEBK";
                frmed.bs = tspdtEBKBindingSource;
                frmed.dt = this.timsDS.tsp_dtEBK;
            }
            //frmed.intCodeID = intParentID;
            frmed.strADDorEDIT = "edit";

            if (frmed.ShowDialog()==System.Windows.Forms.DialogResult.OK)
            {
                btnRefreshDep_Click(sender,e);
            }
        }

        private void btnAddEBK_Click(object sender, EventArgs e)
        {
            frmEdit_EBKFBK frmed = new frmEdit_EBKFBK();
            frmed.Text = "Добавление";

            if (ckbFBK.Checked)
            {
                frmed.strSPName = "tsp_dtFBK";
                frmed.bs = tspdtFBKBindingSource;
                frmed.dt = this.timsDS.tsp_dtFBK;
            }
            else
            {
                frmed.strSPName = "tsp_dtEBK";
                frmed.bs = tspdtEBKBindingSource;
                frmed.dt = this.timsDS.tsp_dtEBK;
            }
            //frmed.intCodeID = intParentID;
            frmed.strADDorEDIT = "add";

            if (frmed.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                btnRefreshDep_Click(sender, e);
            }
        }

        private void mnuExpand_Click(object sender, EventArgs e)
        {
            treeList1.ExpandAll();
            //treeList1.CollapseAll();
        }

        private void mnuCollapse_Click(object sender, EventArgs e)
        {
            treeList1.CollapseAll();
        }
    }
}
